package com.ihm;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.*;

import java.io.File;
import java.sql.SQLException;

import javax.swing.JFileChooser;
import javax.swing.filechooser.*;

import com.ihm.FenetreEdition;
import com.dao.CartePreparedRequete;
import com.dao.Connecter;

public class FenetreModification extends JDialog  {
	private JLabel labelmodifier ;
	private JLabel labeltitre ;
	private JLabel labelid ;
	private JLabel labeldescription ;
	private JLabel labelimage ;
	private static JTextField texttitre ;
	private static JTextArea textdescription ;
	private static JTextField textimage ;
	private static JTextField text_id ;
	public static JTextField getText_id() {
		return text_id;
	}
	public static void setText_id(JTextField text_id) {
		FenetreModification.text_id = text_id;
	}
	private CartePreparedRequete carteps ;
	private Connecter connector ;
	public static JTextField getTextimage() {
		return textimage;
	}
	public static void setTextimage(JTextField textimage) {
		FenetreModification.textimage = textimage;
	}
	private JButton buttonparcourir ;
	private JButton buttonmodifier ;
	private JButton buttonsupprimer ;
	private JButton buttoneffacer ;
	private JButton buttonannuler ;
	private JPanel panelid;
	private JPanel panelhaut ;
	private JPanel paneltitre ;
	private JPanel paneldescription ;
	private JPanel panelimage ;
	private JFileChooser file ;
	public static JTextField getTexttitre() {
		return texttitre;
	}
	public static void setTexttitre(JTextField texttitre) {
		texttitre = texttitre;
	}
	public static JTextArea getTextdescription() {
		return textdescription;
	}
	public static void setTextdescription(JTextArea textdescription) {
		textdescription = textdescription;
	}
	private JPanel panelbuton ;
	private JPanel mainpanel ;
	private Integer Id_carte ;
	public FenetreModification()
	{
		this.setTitle("Modification de la carte");
		this.setSize(600, 400);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.setModal(true);
		createpanel();
		createcomponent();
        addcomponent();
		this.add(mainpanel);
		event();
	}
	public void createcomponent()
	{   labelid = new JLabel("ID");
        text_id = new JTextField();
        text_id.setMaximumSize(new Dimension(60,20));
        text_id.setSize(60,20);
        text_id.setEditable(false);
		labelmodifier = new JLabel("Modifier Lieu");
		labelmodifier.setFont(new Font("italique",Font.BOLD,20));
	    labeltitre = new JLabel("titre");
	    labeltitre.setFont(new Font("italique",Font.BOLD,18));
	    texttitre = new JTextField();
	    texttitre.setMaximumSize(new Dimension(150,20));
	    texttitre.setSize(150,20);
	    labeldescription = new JLabel("Description");
	    labeldescription.setFont(new Font("italique",Font.BOLD,18));
	    textdescription = new JTextArea();
	    textdescription.setMaximumSize(new Dimension(200,100));
	    textdescription.setSize(200,100);
	    labelimage = new JLabel("Image");
	    labelimage.setFont(new Font("italique",Font.BOLD,18));
	    textimage = new JTextField();
	    textimage.setMaximumSize(new Dimension(150,20));
	    textimage.setSize(150,20);
	    textimage.setEditable(false);
	    buttonparcourir = new JButton("parcourir");
	    buttonparcourir.setMaximumSize(new Dimension(60,20));
	    buttonparcourir.setSize(60, 20);
	    buttonmodifier = new JButton("modifier");
	    buttonmodifier.setMaximumSize(new Dimension(60,20));
	    buttonmodifier.setSize(60, 20);
	    buttonsupprimer = new JButton("supprimer");
	    buttonsupprimer.setMaximumSize(new Dimension(60,20));
	    buttonsupprimer.setSize(60,20);
	    buttoneffacer = new JButton("effacer");
	    buttoneffacer.setMaximumSize(new Dimension(60,20));
	    buttoneffacer.setSize(60,20);
	    buttonannuler = new JButton("annuler");
	    buttonannuler.setMaximumSize(new Dimension(60,20));
	    buttonannuler.setSize(60, 20);
	    
	    
	}
	public void createpanel()
	{   panelid = new JPanel();
	     panelid.setLayout(new BoxLayout(panelid,BoxLayout.X_AXIS));
		panelhaut = new JPanel();
		paneltitre = new JPanel();
		paneltitre.setLayout(new BoxLayout(paneltitre,BoxLayout.X_AXIS));
		paneldescription = new JPanel();
		paneldescription.setLayout(new BoxLayout(paneldescription,BoxLayout.X_AXIS));
		panelimage = new JPanel();
		panelimage.setLayout(new BoxLayout(panelimage,BoxLayout.X_AXIS));
		panelbuton = new JPanel();
		panelbuton.setLayout(new BoxLayout(panelbuton,BoxLayout.X_AXIS));
		mainpanel = new JPanel();
		mainpanel.setLayout(new BoxLayout(mainpanel,BoxLayout.Y_AXIS));
	}
	public void addcomponent()
	{   
		panelid.add(labelid);
		panelid.add(Box.createHorizontalStrut(20));
	    panelid.add(text_id);
		panelhaut.add(labelmodifier);
		paneltitre.setLayout(new BoxLayout(paneltitre,BoxLayout.X_AXIS));
		paneltitre.add(labeltitre);
		paneltitre.add(Box.createHorizontalStrut(20));
		paneltitre.add(texttitre);
		paneldescription.add(labeldescription);
		paneldescription.add(Box.createHorizontalStrut(20));
		paneldescription.add(textdescription);
		panelimage.add(labelimage);
		panelimage.add(Box.createHorizontalStrut(20));
		panelimage.add(buttonparcourir);
		panelimage.add(Box.createHorizontalStrut(20));
		panelimage.add(textimage);
		panelbuton.add(Box.createHorizontalGlue());
		panelbuton.add(buttonmodifier);
		panelbuton.add(Box.createHorizontalStrut(20));
		panelbuton.add(buttonsupprimer);
		panelbuton.add(Box.createHorizontalStrut(20));
		panelbuton.add(buttoneffacer);
		panelbuton.add(Box.createHorizontalStrut(20));
		panelbuton.add(buttonannuler);
		panelbuton.add(Box.createHorizontalStrut(20));
		mainpanel.add(panelhaut);
		mainpanel.add(panelid);
		mainpanel.add(paneltitre);
		mainpanel.add(paneldescription);
		mainpanel.add(panelimage);
		mainpanel.add(panelbuton);
	}
    public void event()
    {
    	buttonmodifier.addMouseListener(new MouseListener(){

			@Override
			public void mouseClicked(MouseEvent arg0) {
				// TODO Auto-generated method stub
				carteps = new CartePreparedRequete();
				try{
					carteps.modifier();
				}
				catch(SQLException e)
				{
					e.printStackTrace();
					throw new RuntimeException("L'erreur dans le script de SQL");
				}
				finally
				{
					connector.closeconnection();
				}
			}

			@Override
			public void mouseEntered(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseExited(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mousePressed(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseReleased(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
    		
    	});
    	buttonparcourir.addMouseListener(new MouseListener(){
        
			@Override
			public void mouseClicked(MouseEvent arg0) {
				// TODO Auto-generated method stub
				file = new JFileChooser(".");
				FileNameExtensionFilter filter = new FileNameExtensionFilter("Choisir un fichier image","png","jpg","gif");
				file.setFileFilter(filter);
				int result = file.showOpenDialog(null);
				textimage.setText(null);
				if(result==JFileChooser.APPROVE_OPTION)
				{
					String path1 =file.getSelectedFile().getAbsolutePath();
					textimage.setText(path1);
				}
			}

			@Override
			public void mouseEntered(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseExited(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mousePressed(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseReleased(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
    		
    	});
        buttoneffacer.addMouseListener(new MouseListener(){
			@Override
			public void mouseClicked(MouseEvent arg0) {
				// TODO Auto-generated method stub
				texttitre.setText(null);
				textdescription.setText(null);
				textimage.setText(null);
				JOptionPane.showMessageDialog(null, "Veuillez remplir les champs");
			}

			@Override
			public void mouseEntered(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseExited(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mousePressed(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseReleased(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
        	
        });
        buttonannuler.addMouseListener(new MouseListener(){
			@Override
			public void mouseClicked(MouseEvent arg0) {
				// TODO Auto-generated method stub
				close();
			}

			@Override
			public void mouseEntered(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseExited(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mousePressed(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseReleased(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
        	
        });
        buttonsupprimer.addMouseListener(new MouseListener(){

			@Override
			public void mouseClicked(MouseEvent arg0) {
				// TODO Auto-generated method stub
				carteps = new CartePreparedRequete();
				try {
					carteps.supprimer();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				finally
				{
					close();
				}
			}

			@Override
			public void mouseEntered(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseExited(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mousePressed(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseReleased(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
        	
        });
    }
    public void close()
    {
    	this.dispose();
    }
}
